﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GEAR_Structure;

namespace Gear_Base
{
    public partial class SelectMaterial : Form
    {

        private Project project;
        private Materiale tmp;
        private Materiale selected;

        public SelectMaterial(Project _project)
        {
            InitializeComponent();
            this.project = _project;
            this.tmp = null;
            this.selected = null;
            btnSelect.Enabled = false;
            loadProject();
        }

        private void loadProject()
        {
            foreach (Category_material cm in project.Materials)
            {
                TreeNode node = new TreeNode(cm.Description);
                node.Tag = cm;
                treeMaterial.Nodes[0].Nodes.Add(node);
                foreach (Materiale m in cm.ListMaterial)
                {
                    TreeNode nodeMaterial = new TreeNode(m.Name);
                    nodeMaterial.Tag = m;
                    node.Nodes.Add(nodeMaterial);
                }
            }
        }

        public Materiale getSelected(Form parent)
        {
            base.ShowDialog(parent);
            return selected;
        }

        private void treeMaterial_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeMaterial.SelectedNode.Level == 2)
                tmp = new Materiale((Materiale)treeMaterial.SelectedNode.Tag);
            else
                tmp = null;
            btnSelect.Enabled=(tmp!=null);
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            this.selected = tmp;
            this.Close();
        }

    }
}
